package com.ruoyi.biz.controller;

import com.ruoyi.biz.dto.LotteryPrizeDTO;
import com.ruoyi.biz.dto.LotteryResultDTO;
import com.ruoyi.biz.entity.LotteryPrizeDO;
import com.ruoyi.biz.entity.LotteryResultDO;
import com.ruoyi.biz.service.LotteryPrizeService;
import com.ruoyi.biz.service.LotteryResultService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.util.ValidateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/biz/lotteryPrize")
public class LotteryPrizeController extends BaseController {

    private String prefix = "lotteryPrize";

    @Resource
    LotteryPrizeService lotteryPrizeService;
    @Resource
    LotteryResultService lotteryResultService;

    @GetMapping("/{lotteryId}")
    public String build(@PathVariable("lotteryId") String lotteryId, ModelMap mmap) {
        mmap.put("lottery", lotteryId);
        return prefix + "/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LotteryPrizeDTO lotteryPrizeDTO) {
        startPage();
        List<LotteryPrizeDO> list = lotteryPrizeService.queryList(lotteryPrizeDTO);
        for (LotteryPrizeDO item : list) {
            item.setPrizeImgBase64String(new String(item.getPrizeImgByte()));
        }
        return getDataTable(list);
    }

    /**
     * 新增用户
     */
    @GetMapping("/add/{lotteryId}")
    public String add(@PathVariable("lotteryId") String lotteryId, ModelMap mmap) {
        mmap.put("lottery", lotteryId);
        return prefix + "/add";
    }

    @Log(title = "抽奖奖品管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LotteryPrizeDTO lotteryPrizeDTO) {
        if (ValidateUtil.isEmpty(lotteryPrizeDTO)) {
            return error("新增抽奖奖项的入参不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getLotteryId())) {
            return error("新增抽奖活动的抽奖活动ID不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getLotteryPrizeTitle())) {
            return error("新增抽奖活动的抽奖奖项名称不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getLotteryPrizeName())) {
            return error("新增抽奖活动的抽奖奖品名称不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getPrizeCnt())) {
            return error("新增抽奖活动的奖品数量不能为空！");
        }
        if (lotteryPrizeDTO.getPrizeCnt() < 1) {
            return error("新增抽奖活动的奖品数量必须大于0！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getPrizeRate())) {
            return error("新增抽奖活动的奖品中奖率不能为空！");
        }
        if (lotteryPrizeDTO.getPrizeRate() < 1 || lotteryPrizeDTO.getPrizeRate() > 10000) {
            return error("新增抽奖活动的奖品中奖率必须在1到10000之间！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getPrizeImgName())) {
            return error("新增抽奖活动的奖品图片信息不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getPrizeImgBase64String())) {
            return error("新增抽奖活动的奖品图片信息不能为空！");
        }
        //判断和其他抽奖活动是否有时间交叉
        LotteryPrizeDTO qLotteryPrizeDTO = new LotteryPrizeDTO();
        qLotteryPrizeDTO.setLotteryId(lotteryPrizeDTO.getLotteryId());
        qLotteryPrizeDTO.setLotteryPrizeTitle(lotteryPrizeDTO.getLotteryPrizeTitle());
        qLotteryPrizeDTO.setValiFlag("1");
        List<LotteryPrizeDO> list = lotteryPrizeService.queryList(qLotteryPrizeDTO);
        if (!ValidateUtil.isEmpty(list)) {
            return error("已存在此奖项，请勿重复添加！");
        }
        lotteryPrizeDTO.setPrizeImgByte(lotteryPrizeDTO.getPrizeImgBase64String().getBytes());
        lotteryPrizeDTO.setCrterId(getSysUser().getUserId().toString());
        lotteryPrizeDTO.setCrterName(getSysUser().getUserName());
        lotteryPrizeDTO.setCrteTime(new Date());
        lotteryPrizeDTO.setOpterId(getSysUser().getUserId().toString());
        lotteryPrizeDTO.setOpterName(getSysUser().getUserName());
        lotteryPrizeDTO.setOptTime(new Date());
        return toAjax(lotteryPrizeService.insertLotteryPrize(lotteryPrizeDTO));
    }

    /**
     * 修改抽奖活动信息
     */
    @GetMapping("/edit/{lotteryPrizeId}")
    public String edit(@PathVariable("lotteryPrizeId") String lotteryPrizeId, ModelMap mmap) {
        LotteryPrizeDTO lotteryPrizeDTO = lotteryPrizeService.queryById(lotteryPrizeId);
        lotteryPrizeDTO.setPrizeImgBase64String(new String(lotteryPrizeDTO.getPrizeImgByte()));
        mmap.put("lotteryPrize", lotteryPrizeDTO);
        return prefix + "/edit";
    }

    @Log(title = "抽奖奖品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LotteryPrizeDTO lotteryPrizeDTO) {
        if (ValidateUtil.isEmpty(lotteryPrizeDTO)) {
            return error("修改抽奖奖品的入参不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getLotteryPrizeId())) {
            return error("修改抽奖活动的抽奖奖项ID不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getLotteryId())) {
            return error("修改抽奖活动的抽奖活动ID不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getLotteryPrizeTitle())) {
            return error("修改抽奖活动的抽奖奖项名称不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getLotteryPrizeName())) {
            return error("修改抽奖活动的抽奖奖品名称不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getPrizeCnt())) {
            return error("修改抽奖活动的奖品数量不能为空！");
        }
        if (lotteryPrizeDTO.getPrizeCnt() < 1) {
            return error("修改抽奖活动的奖品数量必须大于0！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getPrizeRate())) {
            return error("修改抽奖活动的奖品中奖率不能为空！");
        }
        if (lotteryPrizeDTO.getPrizeRate() < 1 || lotteryPrizeDTO.getPrizeRate() > 10000) {
            return error("修改抽奖活动的奖品中奖率必须在1到10000之间！");
        }

        //判断修改的奖项是否已存在
        LotteryPrizeDTO qLotteryPrizeDTO = new LotteryPrizeDTO();
        qLotteryPrizeDTO.setLotteryId(lotteryPrizeDTO.getLotteryId());
        qLotteryPrizeDTO.setLotteryPrizeTitle(lotteryPrizeDTO.getLotteryPrizeTitle());
        qLotteryPrizeDTO.setValiFlag("1");
        List<LotteryPrizeDO> lotteryPrizeDTOS = lotteryPrizeService.queryList(qLotteryPrizeDTO);
        if (!ValidateUtil.isEmpty(lotteryPrizeDTOS)) {
            //排除自己
            lotteryPrizeDTOS = lotteryPrizeDTOS.stream().filter(item -> !lotteryPrizeDTO.getLotteryPrizeId().equals(item.getLotteryPrizeId())).collect(Collectors.toList());
            if (!ValidateUtil.isEmpty(lotteryPrizeDTOS)) {
                return error("修改的奖品奖项名称已存在，不能修改！");
            }
        }

        LotteryResultDTO qLotteryResultDTO = new LotteryResultDTO();
        qLotteryResultDTO.setLotteryId(lotteryPrizeDTO.getLotteryId());
        qLotteryResultDTO.setLotteryPrizeId(lotteryPrizeDTO.getLotteryPrizeId());
        qLotteryResultDTO.setValiFlag("1");
        List<LotteryResultDO> lotteryResultDTOS = lotteryResultService.queryList(qLotteryResultDTO);

        // 当前活动的每项奖品的中奖数量是否小于维护的数量
        int lotterResultCnt = ValidateUtil.isEmpty(lotteryResultDTOS) ? 0 : lotteryResultDTOS.size();
        if (lotteryPrizeDTO.getPrizeCnt() < lotterResultCnt) {
            return error(lotteryPrizeDTO.getLotteryPrizeTitle() + "已经有" + lotterResultCnt + "人中奖，因此奖品数量不能小于" + lotterResultCnt + "！");
        }

        if (!ValidateUtil.isEmpty(lotteryPrizeDTO.getPrizeImgBase64String())) {
            lotteryPrizeDTO.setPrizeImgByte(lotteryPrizeDTO.getPrizeImgBase64String().getBytes());
        }
        lotteryPrizeDTO.setOpterId(getSysUser().getUserId().toString());
        lotteryPrizeDTO.setOpterName(getSysUser().getUserName());
        lotteryPrizeDTO.setOptTime(new Date());
        return toAjax(lotteryPrizeService.updateLotteryPrize(lotteryPrizeDTO));
    }


    @Log(title = "抽奖奖品管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        LotteryPrizeDTO qLotteryPrizeDTO = new LotteryPrizeDTO();
        qLotteryPrizeDTO.setLotteryPrizeId(ids);
        qLotteryPrizeDTO.setValiFlag("1");
        List<LotteryPrizeDO> lotteryPrizeDTOS = lotteryPrizeService.queryList(qLotteryPrizeDTO);
        if (ValidateUtil.isEmpty(lotteryPrizeDTOS)) {
            return error("系统错误，查询奖品信息失败！");
        }

        LotteryResultDTO qLotteryResultDTO = new LotteryResultDTO();
        qLotteryResultDTO.setLotteryId(lotteryPrizeDTOS.get(0).getLotteryId());
        qLotteryResultDTO.setLotteryPrizeId(ids);
        qLotteryResultDTO.setValiFlag("1");
        List<LotteryResultDO> lotteryResultDTOS = lotteryResultService.queryList(qLotteryResultDTO);
        if (!ValidateUtil.isEmpty(lotteryResultDTOS)) {
            return error(lotteryPrizeDTOS.get(0).getLotteryPrizeTitle() + "已经有人中奖，无法删除！");
        }
        return toAjax(lotteryPrizeService.deleteLotteryPrize(ids));
    }
}
